package com.heima.article.listen;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.NewsUpOrDownConstants;
import com.heima.common.constants.message.PublishArticleConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 监听来自WmNewsServiceImpl的上下架信息
 * article文章上下架更改config表中的信息
 * @author lo
 * @date 2021/9/27
 */
@Component
@Slf4j
public class ArticleUpOrDownListener {
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @RabbitListener(queuesToDeclare =
            //监听文章上架队列
    @Queue(value = NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE)
    )
    public void newsUpHandler(@Payload String articleId, Message message, Channel channel) {
        log.debug("接收文章上架通知， 文章id: {} , 当前时间: {}",articleId, LocalDateTime.now().toString());
        //添加对ApArticleConfig apArticleConfig = new ApArticleConfig();
        // 当前消息路由
        try {
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    //是否上架
                            .eq(ApArticleConfig::getArticleId,Long.valueOf(articleId))
                            .set(ApArticleConfig::getIsDown,false)
                        );
            // 手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("article处理 自媒体文章上架消息 失败, 文章id: {}   原因: {}",articleId,e.getMessage());
        }

    }

    @RabbitListener(queuesToDeclare =
            //监听文章下架队列
    @Queue(value = NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE)
    )
    public void newsDownHandler(@Payload String articleId, Message message, Channel channel) {
        log.debug("接收文章下架通知， 文章id: {} , 当前时间: {}",articleId, LocalDateTime.now().toString());
        //添加对ApArticleConfig apArticleConfig = new ApArticleConfig();
        // 当前消息路由
        try {
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    //是否下架
                    .eq(ApArticleConfig::getArticleId,Long.valueOf(articleId))
                    .set(ApArticleConfig::getIsDown,true));
            // 手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("article处理 自媒体文章上架消息 失败, 文章id: {}   原因: {}",articleId,e.getMessage());
        }
    }
}
